<?php
/**
 * @version SVN: $Id$
 * @package    fmcSeminaire
 * @subpackage Views
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Ludovic Launer {@link }
 * @author     Created on 09-Nov-2010
 * @license    GNU/GPL
 */
$document = &JFactory::getDocument();
//$document->setCharset('iso-8859-1');

$document->addScript('includes/js/jquery/jquery.js' );
$document->addScript('includes/js/jquery/jquery.json-2.2.js' );
$document->addScript('includes/js/jquery/jquery-validate/jquery.validate.js' );
$document->addScript('includes/js/jquery/jquery-validate//localization/messages_fr.js' );

$document->addScript('includes/js/jquery/jquery-ui-1.8.6.custom/js/jquery-ui-1.8.6.custom.min.js' );
$document->addScript('includes/js/jquery/jquery-ui-1.8.6.custom/development-bundle/ui/i18n/jquery.ui.datepicker-fr.js' );

$document->addStyleSheet('includes/js/jquery/jquery-ui-1.8.6.custom/css/ui-lightness/jquery-ui-1.8.6.custom.css');

$document->addScript('includes/js/jquery/jquery.datePicker/date.js' );
$document->addScript('includes/js/jquery/jquery.datePicker/date_fr.js' );
$document->addScript('includes/js/jquery/jquery.datePicker/jquery.datePicker.js' );
$document->addStyleSheet('includes/js/jquery/jquery.datePicker/datePicker.css');

$document->addStyleSheet('includes/css/ludovic_table_style.css');

?>
<script type="text/javascript">
//------------------------------------------------------------------------------------

	$.fn.serializeObject = function()
		{
		 var o = {};
		 var a = this.serializeArray();
		 $.each(a, function() {
		 if (o[this.name]) {
		 if (!o[this.name].push) {
		 o[this.name] = [o[this.name]];
		 }
		 o[this.name].push(this.value || '');
		 } else {
		 o[this.name] = this.value || '';
		 }
		 });
		 return o;
	};
// ------------------------------------------------------------------------------------  
function voteSuccess() {
	$('#success').fadeIn(200).show();
	$('.error').fadeOut(200).hide();
	$('#voteButton').attr("disabled", true);
	window.location.hash="up"; 
}
// ------------------------------------------------------------------------------------ 	
$(document).ready(function(){ 

	
	$("#ajax-form").submit(function(){
		var nbfields = $("div#seminars").children().filter("input").length;
		alert(nbfields);

		
		alert($('#your_input').val());

		
		$.ajax({
				type: "POST",
				dataType: "json",
				url: "index.php?option=com_fmcseminaire&task=sbmitWishes",
				data: ({"toto":1, "titi":2}),
				success: voteSuccess()
				}); 
   	});
// ------------------------------------------------------------------------------------ 
	//$("#form_vote").submit(function(){
	function submit_form_vote(){	
		var form_vote_data = $("#form_vote").serializeObject(); 

		$.ajax({
			type: "POST",
			async: false,
			dataType: "json",
			url: "index.php?option=com_fmcseminaire&task=sbmitWishes",
			data: form_vote_data,
			success:ajaxVoteSuccess,
			error: function()
				{
				// Request failed: Do something
				}
			}); 
	}
   	//});
// ------------------------------------------------------------------------------------ 
function ajaxVoteSuccess(data)
{
	if (data.success==true) {
		$('.success').fadeIn(200).show();
		$('.error').fadeOut(200).hide();
		$( "#dialog-message" ).dialog({
			modal: true,
			resizable: false,
			buttons: {
				Ok: function() {
					$( this ).dialog( "close" );
					voteSuccess();
					}
				}
		})
		$('#voteButton').attr("disabled", true); 
	}
	else {
		alert("Attention :\n" + data.message);
		if (data.stop)								// Prevent user from voting
			voteSuccess();
	}
}
// ------------------------------------------------------------------------------------ 
// --- Data validataion before submiting the form via Ajax ---

$("#form_vote").validate({
	 submitHandler: function(form) {
	   // some other code
	   // maybe disabling submit button
	   // then:
	   submit_form_vote();
	 },
	 rules: {
		 codeadeli: {
		      required: true,
		      digits: true,
		      minlength: 9,
		      maxlength: 9
		    }
		  }
	  
	});
//------------------------------------------------------------------------------------ 
});
//------------------------------------------------------------------------------------ 
function setupDatepicker(id_datepicker, dateDebut, dateFin)
{
	$(id_datepicker)
	.datePicker({
				inline:true,
				selectMultiple: true
				})
	.bind('dateSelected',
		function(e, selectedDate, $td)
			{
			//console.log('You selected ' + selectedDate);
			}
	);

	var curr_date 	= Date.fromString(dateDebut);
	var fin_date 	= Date.fromString(dateFin);

	if (curr_date <= fin_date) {									// Just in case...
		while (curr_date <= fin_date) {
			$(id_datepicker).dpSetSelected(curr_date.asString());	// Select day
			curr_date = curr_date.addDays(1)
		}
	$(id_datepicker).dpSetStartDate(dateDebut);						// Restrict selection
	$(id_datepicker).dpSetEndDate(dateFin);
	}
	
}
//------------------------------------------------------------------------------------ 
</script> 


<?php 
//-- No direct access
defined('_JEXEC') or die('=;)');

?>
<style type="text/css">
<!--
#voteButton {
		width:200px;
		height:50px;
		position:relative;
		left:-100px;
		font-size: 200%;
}
-->
</style>

<!--  ----------------------------------------------------------------------------- -->
<span id="success" class="componentheading" style="display:none; color:green">Votre vote a &eacute;t&eacute; pris en compte.</span>
<h4>
Ce formulaire vous permet de nous soumettres vos choix de s&eacute;minaires et dates de disponibilit&eacute;
</h4>
<br>
<!--  ----------------------------------------------------------------------------- -->
<style type="text/css">
	label { width: 10em; float: left; }
	label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
	p { clear: both; }
	.submit { margin-left: 12em; }
	em { font-weight: bold; padding-right: 1em; vertical-align: top; }
</style>
 
<form id="form_vote" onsubmit="return false;">
   
   <p>
     <label for="cname">Nom Pr&eacute;nom</label>
     <em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" />
   </p>
   <p>
     <label for="cemail">E-Mail</label>
     <em>*</em><input id="cemail" name="email" size="25"  class="required email" />
   </p>
   <p>
     <label for="ccodeadeli">Code ADELI</label>
     <em>*</em><input id="codeadeli" name="codeadeli" size="8"/>
   </p>
  
<!--  ----------------------------------------------------------------------------- -->

<label style="margin-top:20px;">Campagne</label>
<div class="componentheading" style="text-align:center;margin:20px;">
 <?php echo $this->data[0]->campagneName;?>
</div>
<input type="hidden" value=" <?php echo $this->data[0]->campagneId;?>" name="campagneId">

<h1>Choix du Seminaire</h1>
<table summary="Choix du Seminaire" class="hor-minimalist-b">
    <thead>
    	<tr>
        	<th scope="col" style="width:20px"></th>
            <th scope="col">Seminaire</th>
        </tr>
    </thead>
    
	<tbody>
	<?php 
	
	foreach($this->data as $data) :						// Browse through Seminaires
		$seminaire 	= $data->greeting;
		$content_id = $data->content_id;
	?>
		<tr>
        	<td>
        	
				<input type="checkbox" name="submitedSeminaires[]" value="<?php echo $data->seminaireId;?>">
			</td>
		
			<td>
				<?php if($content_id==null || $content_id=='' ): ?>
					<?php echo $seminaire?>
				<?php else: ?>
					 {modal url=index.php?option=com_content&view=article&id=<?php echo $content_id?>|width=500|height=300}<?php echo $seminaire?>{/modal}
				<?php endif; ?>

			</td>
		 </tr>
	<?php
	endforeach;
	?>
	</tbody>
</table>
<!--  ----------------------------------------------------------------------------- -->	
<h1>Choix des Dates</h1>
<table summary="Choix des dates" class="hor-minimalist-b">
    <thead>
    	<tr>
        	<th scope="col" style="width:20px"></th>
            <th scope="col">Dates de disponibilit&eacute;</th>
            <th scope="col"></th>
        </tr>
    </thead>
    
	<tbody>
	<?php
	$dp=0;
	foreach($this->seminaireDates as $semDate) :			// Browse through seminaire_dates
		$dp+=1;
	?>
		<tr>
        	<td>
				<input type="checkbox" name="submitedDates[]" value="<?php echo $semDate->dateId;?>">
			</td>
		
			<td>
			
				<?php
				if ($semDate->nbJours>1)
					echo "Du " . $semDate->dateDebut . " au " . $semDate->dateFin;
				else
				 	echo $semDate->dateDebut;
				?>
			</td>
			<td>
				<div id="<?php echo 'dp' . $dp;?>" style="float:right" ></div>
				<script type="text/javascript">
					setupDatepicker('<?php echo '#dp' . $dp;?>',
									'<?php echo $semDate->dateDebut;?>',
									'<?php echo $semDate->dateFin;?>');
				</script>
			</td>
		</tr>		 
	<?php
	endforeach;
	?>
	</tbody>
</table>
<!--  ----------------------------------------------------------------------------- -->	
<p>
<center><input id="voteButton" value="Voter" type="submit" ></center>
</p>

</form>

<span class="error" style="display:none; color:red"> Erreur pendant la transaction</span>
<!--  ----------------------------------------------------------------------------- -->	
<br>

<!--  ----------------------------------------------------------------------------- -->	
<div id="dialog-message" title="Vote prise en compte" style="display:none">
	<p>
		<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
		Votre vote a ete pris en compte.
	</p>
	<p>
		Merci pour votre participation.
	</p>
</div>
<!--  ----------------------------------------------------------------------------- -->	


